package com.atpy.reggie.controller;

import com.atpy.reggie.common.BaseContext;
import com.atpy.reggie.common.R;
import com.atpy.reggie.entity.Coupon;
import com.atpy.reggie.entity.CouponUser;
import com.atpy.reggie.service.CouponService;
import com.atpy.reggie.service.CouponUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/coupon")
@Slf4j
public class CouponController {

    @Autowired
    private CouponUserService couponUserService;
    @Autowired
    private CouponService couponService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){

        Page<Coupon> couponPage = new Page<>(page,pageSize);

        Page<Coupon> Page = couponService.page(couponPage);
        return R.success(Page);

    }
/*
function queryCouponById (id) {
        return $axios({
                url: `/coupon/queryCouponById/${id}`,
        method: 'get'
  })
    }
 */
@GetMapping("/queryCouponById/{couponId}")
public R<Coupon> queryCouponById(@PathVariable("couponId") Long coupId){

    Coupon coupon = couponService.getById(coupId);
    return R.success(coupon);

}
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Coupon coupon){
        log.info("新增优惠卷:{}",coupon.toString());
        couponService.save(coupon);
        return R.success("新增优惠卷成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Coupon coupon,HttpServletRequest request){
        log.info("更改优惠卷{}",coupon.toString());
//        Long empid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(empid);
        couponService.updateById(coupon);
        return R.success("切换成功");
    }

    @GetMapping("/list")
    public R<List> page(){
        LambdaQueryWrapper<CouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponUser::getUserId,BaseContext.getCurrentId());
        List<CouponUser> couponUsers = couponUserService.list(wrapper);
        List<Coupon> coupons = couponService.list();
//        for (int i = coupons.size(); i > -1; i--) {
//            for (CouponUser couponUser : couponUsers) {
//                if (couponUser.getCouponTempId() == coupons.get(i).getId()){
//                    coupons.remove(i);
//
//                }
//            }
//        }


        Iterator<Coupon> iterator = coupons.iterator();
        while (iterator.hasNext()){
            long id = iterator.next().getId();
            for (CouponUser couponUser : couponUsers) {
                if (couponUser.getCouponTempId()==id){
                    iterator.remove();
                }
            }
        }


        return R.success(coupons);

    }

    /*
    //用户领取优惠卷
    function  getCoupon(data){
        return $axios({
                'url': '/coupon/receiveCoupon',
                'method': 'post',
                data
    })
    }
     */

    @PostMapping("receiveCoupon")
    public R<String> receiveCoupon(HttpServletRequest request, @RequestBody Coupon coupon){
        CouponUser couponUser = new CouponUser();

        log.info("优惠卷:{}",coupon.toString());
        Long UserId = BaseContext.getCurrentId();

        couponUser.setUserId(UserId);
        couponUser.setCouponTempId(coupon.getId());
        couponUser.setCouponType(coupon.getCouponType());
        couponUser.setCouponAmount(coupon.getCouponAmout());
        couponUser.setFullAmount(coupon.getFullAmount());
        couponUser.setEffectiveStartTime(coupon.getEffectiveStartTime());
        couponUser.setEffectiveEndTime(coupon.getEffectiveEndTime());
        couponUser.setStatus(0);

        LambdaQueryWrapper<CouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CouponUser::getUserId,UserId);
        wrapper.eq(CouponUser::getCouponTempId,coupon.getId());
        int count = couponUserService.count(wrapper);
        if (count > 0){
            return R.error("请不要重复领取");
        }

        boolean is = couponUserService.save(couponUser);
        if (is){
            return R.success("领取优惠卷成功");
        }
        return R.success("领取优惠卷失败");

    }


}
